//
//  WBComposeButtomView.swift
//  Weibo
//
//  Created by xu on 2017/3/28.
//  Copyright © 2017年 xuantong. All rights reserved.
//

//
//  WBComposeBottomViewType.swift
//  Weibo
//
//  Created by xu on 2017/3/28.
//  Copyright © 2017年 xuantong. All rights reserved.
//

import UIKit


enum WBComposeBottomViewType: Int {
    // 图片
    case picture = 0
    // @
    case mention = 1
    // #
    case trend = 2
    // 笑脸
    case emoticon = 3
    // +
    case add = 4
}

class WBComposeBottomView: UIView {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 记录表情按钮
    var emoticonButton: UIButton?
    
    var closure:((WBComposeBottomViewType)->())?
    // 判断是系统键盘还是自定义表情键盘
      var isEmoticon: Bool = false{
        didSet{
            // 记录图片的名称
            var imgName = ""
            // 如果==true 显示的自定义键盘 -> image -> 小键盘图片
            if isEmoticon {
                   imgName = "compose_keyboardbutton_background"
            }else{
                // 如果==false 显示的系统键盘 -> image -> 笑脸图片
                imgName = "compose_emoticonbutton_background"
            }
            
            //设置按钮d图片
            emoticonButton!.setImage(UIImage(named:imgName), for: UIControlState.normal)
            emoticonButton!.setImage(UIImage(named:"\(imgName)_highlighted"), for: UIControlState.highlighted)

            
        }
        
    }
    
    private func setupUI() {
        
        backgroundColor = UIColor(patternImage: UIImage(named:"compose_toolbar_background")!)
        
        let pictureButton = addChildButtons(imgName: "compose_toolbar_picture", type: .picture)
        let mentionButton = addChildButtons(imgName: "compose_mentionbutton_background", type: .mention)
        let trendButton = addChildButtons(imgName: "compose_trendbutton_background",  type: .trend)
        emoticonButton = addChildButtons(imgName: "compose_emoticonbutton_background",  type: .emoticon)
        let addButton = addChildButtons(imgName: "compose_add_background",  type: .add)
        

        //约束
        pictureButton.snp.makeConstraints { (make) in
            make.top.left.bottom.equalTo(self)
            make.width.equalTo(mentionButton)
        }
        mentionButton.snp.makeConstraints { (make) in
            make.top.bottom.equalTo(self)
            make.width.equalTo(trendButton)
            make.left.equalTo(pictureButton.snp.right)
        }

        trendButton.snp.makeConstraints { (make) in
            make.top.bottom.equalTo(self)
            make.width.equalTo(emoticonButton!)
            make.left.equalTo(mentionButton.snp.right)
        }

        emoticonButton!.snp.makeConstraints { (make) in
            make.top.bottom.equalTo(self)
            make.width.equalTo(addButton)
            make.left.equalTo(trendButton.snp.right)
        }

        addButton.snp.makeConstraints { (make) in
            make.top.right.bottom.equalTo(self)
            make.left.equalTo(emoticonButton!.snp.right)
            
        }

    }
}


extension WBComposeBottomView {
    
    fileprivate func addChildButtons(imgName:String ,type:WBComposeBottomViewType)->UIButton {
        let button = UIButton()
        button.tag = type.rawValue
        button.setImage(UIImage(named:imgName), for: UIControlState.normal)
        button.setImage(UIImage(named:"\(imgName)_highlighted"), for: UIControlState.highlighted)
        button.addTarget(self, action: #selector(buttonClick), for: UIControlEvents.touchUpInside)
        addSubview(button)
        return button
        
        
        
    }
  
    
}

extension WBComposeBottomView {
 
    @objc fileprivate func buttonClick(button:UIButton){
        print("5按钮")
        closure?(WBComposeBottomViewType(rawValue: button.tag)!)
        
    }



}
